package com.hornsun.manage.information.api;

import com.hornsun.domain.constant.types.InfoType;
import com.hornsun.domain.dbo.ContentType;
import com.hornsun.domain.dbo.Information;
import com.hornsun.domain.dto.FuzzyQueryDTO;
import com.hornsun.manage.information.dto.InfoDTO;
import com.hornsun.manage.information.dto.MoreInfoDTO;
import com.hornsun.repository.ContentTypeRepository;
import com.hornsun.repository.InformationRepository;
import com.hornsun.repository.mapper.InformationMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by 编译中... on 2018/5/7.
 */
@RestController
@RequestMapping("/manage/information")
public class InformationApi {

    @Resource
    private ContentTypeRepository contentTypeRepository;

    @Resource
    private InformationRepository informationRepository;

    @Resource
    private InformationMapper informationMapper;

    @GetMapping("/getInformation")
    public ResponseEntity<InfoDTO> getInformation(){
        List<ContentType> types = contentTypeRepository.queryContentType(InfoType.INFORMATION.getType());
        if (types.size()==0||types.isEmpty()){
            return new ResponseEntity<InfoDTO>(HttpStatus.OK);
        }
        InfoDTO infoDTO = new InfoDTO();
        infoDTO.setTypes(types);
        ContentType contentType = types.get(0);
        int page = 1;
        infoDTO.setInformationList(informationRepository.getInformationByType(contentType.getId(),page));
        infoDTO.setTotalCount(informationRepository.getCountByTypeid(contentType.getId()));
        return new ResponseEntity<InfoDTO>(infoDTO, HttpStatus.OK);
    }

    @GetMapping("/getMoreInfo")
    public ResponseEntity<?> getMoreInfo(@RequestParam("typeid")String typeId ,@RequestParam("page")Integer page){
        MoreInfoDTO moreInfoDTO = new MoreInfoDTO();
        moreInfoDTO.setInformationList(informationRepository.getInformationByType(typeId,page));
        moreInfoDTO.setTotalCount(informationRepository.getCountByTypeid(typeId));
        return new ResponseEntity<Object>(moreInfoDTO,HttpStatus.OK);
    }


    //资讯发布
    @Transactional
    @PostMapping("/publish")
    public ResponseEntity<?> publishCommodity(@RequestBody Information information){
        if (information.getTypeid().isEmpty()||information.getTypeid()==null){
            return new ResponseEntity<Object>("资讯类型不能为空",HttpStatus.OK);
        }
        if (information.getRecommend()==1){
            informationRepository.resetRecommend(information.getTypeid());
        }
        information.setInformationGuid(UUID.randomUUID().toString());
        information.setHits((int)(Math.random()*30+10));
        information.setPraise((int)(Math.random()*20));
        information.setUpdateTime(new Date());
        informationRepository.addInformation(information);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    //模糊查询
    @GetMapping("/fuzzyQuery")
    public ResponseEntity<FuzzyQueryDTO> fuzzyQuery(@RequestParam("content")String content,@RequestParam("page")int page){
        String description="%"+content+"%";
        FuzzyQueryDTO fuzzyQueryDTO = new FuzzyQueryDTO();
        fuzzyQueryDTO.setList(informationRepository.fuzzyQuery(description,page));
        fuzzyQueryDTO.setCount(informationRepository.countByFuzzyQuery(description));
        return new ResponseEntity<FuzzyQueryDTO>(fuzzyQueryDTO,HttpStatus.OK);
    }



    //置顶
    @Transactional
    @PostMapping("/top")
    public ResponseEntity<?> top(@RequestParam("typeId") String typeId,@RequestParam("guid")String guid ){
        informationRepository.resetRecommend(typeId);
        Information information = new Information();
        information.setInformationGuid(guid);
        information.setRecommend((byte)1);
        informationRepository.updateInformation(information);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    //推荐
    @Transactional
    @PostMapping("/recommend")
    public ResponseEntity<?> recommend(@RequestParam("guid")String informationGuid){
        Information information = new Information();
        information.setInformationGuid(informationGuid);
        information.setHomepangRecommend((byte)1);
        information.setUpdateTime(new Date());
        informationRepository.updateInformation(information);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }


    //删除
    @Transactional
    @PostMapping("/delete")
    public ResponseEntity<?> delete(@RequestParam("guid")String informationGuid){
        informationRepository.deleteInformation(informationGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    @GetMapping("/getAll")
    public ResponseEntity<?> getAll(@RequestParam("page") int page){
        MoreInfoDTO moreInfoDTO = new MoreInfoDTO();
        moreInfoDTO.setTotalCount(informationRepository.getTotalCount());
        moreInfoDTO.setInformationList(informationRepository.getAll(page));
        return new ResponseEntity<Object>(moreInfoDTO,HttpStatus.OK);
    }
}
